<?php
class Controller_Storage_Overseas extends Controller_Abstract
{
	function actionSendCreate()
	{
		$this->_view['title'] = '发送单创建';
		if ($_GET['ids'])
		{
			$this->_view['ids'] = $_GET['ids'];
			$ids = explode(',', $_GET['ids']);
			$purchase_order = Purchase_Order::find("purchase_order.id in (?) ", $ids)
			->joinLeft('purchase_order_item', 'product_basic_id, actual_quantity', 'purchase_order_item.purchase_order_id=purchase_order.id')			
			->asArray()->getAll();
			$product_quantity = 0;
			foreach ($purchase_order as $p)
			{
				$product_quantity += $p['actual_quantity'];
			}
			$this->_view['product_quantity'] = $product_quantity;
		}
		else
		{
			echo "没有找到订单!";
			exit;
		}
	}
	
	function actionSendCreateSave()
	{
		$this->_view['title'] = '发送单创建(保存)';
		if ($this->_context->isPOST())
		{
			if (isset($_POST['purchase_order_ids']) && $_POST['purchase_order_ids'])
			{
				$send_order = array(
					'package_quantity' => $_POST['package_quantity'],
					'product_quantity' => $_POST['product_quantity'],
					'weight' => $_POST['weight'],
					'volume' => $_POST['volume'],
					'express_company' => $_POST['express_company'],
					'express_number' => $_POST['express_number'],
					'tracking_number' => $_POST['tracking_number'],
					'send_time' => $_POST['send_time'],
					'budget_storage_time' => $_POST['budget_storage_time'],
					'actual_storage_time' => $_POST['actual_storage_time'],
					'notes' => $_POST['notes'],
					'purchase_order_ids' => $_POST['purchase_order_ids']
				);
				$result = Overseas_Send::create($send_order);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('保存成功', '正在返回..', url('Storage::Overseas/Sendlist'), $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('保存失败', $result['message'], url('Storage::Overseas/Sendlist'), $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('没有选择订单', '提交信息有误，请重试！', url('Storage::Overseas/Sendlist'), FAILURE);
			}
		}
	}
	
	function actionSendEdit()
	{
		$this->_view['title'] = '发送单编辑';
		$result = Overseas_Send::find('overseas_send.id=?', $_GET['id'])
			->joinLeft('warehouse', '', 'overseas_send.warehouse_id=warehouse.id')
			->columns('warehouse.name as warehouse_name')
			->asArray()->getOne();
		$this->_view['send_order'] = $result;
		$purchase_order_number = array();
		$purchase_order = Overseas_Send_Item::find('overseas_send_id=?', $_GET['id'])
			->joinLeft('purchase_order', 'purchase_order.number', 'purchase_order.id=overseas_send_item.purchase_order_id')
			->asArray()->getAll();
		$this->_view['purchase_order'] = $purchase_order;	
	}
	
	function actionSendEditSave()
	{
		$this->_view['title'] = '发送单编辑(保存)';
		if ($this->_context->isPOST())
		{
			if (isset($_POST['item']) && $_POST['item'])
			{
				$order =array(
					'id' => $_POST['id'],
					'package_quantity' => $_POST['package_quantity'],
					'product_quantity' => $_POST['product_quantity'],
					'weight' => $_POST['weight'],
					'volume' => $_POST['volume'],
					'express_company' => $_POST['express_company'],
					'express_number' => $_POST['express_number'],
					'tracking_number' => $_POST['tracking_number'],
					'send_time' => $_POST['send_time'],
					'budget_storage_time' => $_POST['budget_storage_time'],
					'actual_storage_time' => $_POST['actual_storage_time'],
					'notes' => $_POST['notes'],
					'purchase_order_ids' => $_POST['item'],
				);
				$result = Overseas_Send::storageEdit($order);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('保存成功', '正在返回..', url('Storage::Overseas/Sendlist'), $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('保存失败', '没有绑定采购单', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}		
	}
	
	function actionSendList()
	{
		$this->_view['title'] = '发送单列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$this->_view['warehouse'] = Warehouse::getActiveWarehouse();
		$condition = Helper_BSS_Normal::buildCondition();
		$search_result = Overseas_Send::search($condition, $cp, $ps, 'overseas_send.id desc');
		$page = new Helper_BSS_Page($search_result['pagination'], url('Storage::Overseas/SendList', $condition));
		$this->_view['page'] = $page->getPage();
		Overseas_Send::getPurchaseOrderInfo($search_result['data']);
		$this->_view['send_order'] = $search_result['data'];	
	}
	
	function actionPurchaseOrderAjax()
	{
		$this->_view['title'] = '采购单请求ajax';
		$q = $_REQUEST['q'];
		$provider = Purchase_order::find('number like ? ', '%'.$q.'%')->asArray()->getAll();
		$result = array();
		foreach ($provider as $p)
		{
			$result[] = array(
				0 => $p['number'],
				'id' => $p['id']
			);
		}
		echo json_encode($result);
		exit;		
	}
	
	function actionPurchaseOrderDetail()
	{
		$this->_view['title'] = '采购单详细';
		$url_arr = Helper_BSS_Normal::buildCondition();
		if ($url_arr)
		{
			$purchase_order = Purchase_Order::find('purchase_order.id =?', $url_arr['id'])
				->joinLeft('user', 'name as purchase_user_name', 'user.id = purchase_order.purchase_user_id')
				->joinLeft('warehouse', 'warehouse.name as warehouse_name', 'warehouse.id=purchase_order.warehouse_id')
				->asArray()->getOne();
			$item = Purchase_Order_Item::find('purchase_order_item.purchase_order_id =?', $url_arr['id'])
				->joinLeft('product_basic', 'number as product_number, name, purchase_price', 'product_basic.id = purchase_order_item.product_basic_id')
				->joinLeft('product_warehouse', 'product_warehouse.purchase_budget_quantity, product_warehouse.purchase_quantity, product_warehouse.wholesale_quantity, product_warehouse.purchase_status, product_warehouse.sales_status, product_warehouse.actual_quantity as stock_quantity, product_warehouse.sold_quantity, product_warehouse.safe_quantity, product_warehouse.warning_quantity', 'product_warehouse.product_basic_id=product_basic.id and product_warehouse.warehouse_id=' . $purchase_order['warehouse_id'])
				->joinLeft('product_provider as a', 'name as budget_provider_name', 'a.id = purchase_order_item.budget_provider_id')
				->joinLeft('product_provider as b', 'name as actual_provider_name', 'b.id = purchase_order_item.actual_provider_id')
				->asArray()->getAll();
			Helper_BSS_Normal::getProductImgUrl($item, 'product_number');
			$this->_view['purchase_order'] = $purchase_order;
			$this->_view['item'] = $item;
		}
		else
		{
			return $this->_redirect(url('Storage::Overseas/SendList'));
		}
	}
}